package com.ken.quartz.job;

import jdk.nashorn.internal.scripts.JO;
import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 实现一个最简单的job
 * Created by renchongyu on 2018/11/24.
 */
public class HelloJob implements Job {

    private Float FloatJobValue;
    private Double DoubleTriggerValue;

    public Float getFloatJobValue() {
        return FloatJobValue;
    }

    public void setFloatJobValue(Float floatJobValue) {
        FloatJobValue = floatJobValue;
    }

    public Double getDoubleTriggerValue() {
        return DoubleTriggerValue;
    }

    public void setDoubleTriggerValue(Double doubleTriggerValue) {
        DoubleTriggerValue = doubleTriggerValue;
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        //编写具体的业务逻辑
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time =sdf.format(new Date());
        System.out.println("Current Exec Time is : " +time);
        System.out.println("Hello World!");
        //1、从context中获取JobDetail
        JobDetail jobDetail = context.getJobDetail();
        String name = jobDetail.getKey().getName();
        JobDataMap jobMap = jobDetail.getJobDataMap();//JobDetail中的实现的JobDataMap
        System.out.println("jobName="+name);
        System.out.println("message="+jobMap.getString("message"));
        System.out.println("FloatJobValue="+jobMap.getFloat("FloatJobValue"));
        //2、从context中获取JobDetail
        Trigger trigger = context.getTrigger();
        String triggerName = trigger.getKey().getName();
        JobDataMap triggerMap = trigger.getJobDataMap();//trigger中的实现的JobDataMap
        System.out.println("triggerName="+triggerName);
        System.out.println("message="+triggerMap.getString("message"));
        System.out.println("DoubleTriggerValue="+triggerMap.getDouble("DoubleTriggerValue"));
        //3、可以获得总的DataMap
        JobDataMap allMap = context.getMergedJobDataMap();
        System.out.println("FloatJobValue="+allMap.getFloat("FloatJobValue"));
        System.out.println("DoubleTriggerValue="+allMap.getDouble("DoubleTriggerValue"));

        //4、即使不适用DataMap，可以用set/get得到参数值。(底层肯定使用reflection)
        System.out.println("FloatJobValue="+FloatJobValue);
        System.out.println("DoubleTriggerValue="+DoubleTriggerValue);

        //5、获取任务开始时间、结束时间
        System.out.println(sdf.format(trigger.getStartTime()));
        System.out.println(sdf.format(trigger.getEndTime()));
    }
}
